package com.betterjr.modules.risk.loan.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.modules.risk.loan.dao.LoanTransactionRelationMapper;
import com.betterjr.modules.risk.loan.data.LoanProjectRelationConstantCollections;
import com.betterjr.modules.risk.loan.data.LoanTransactionRelationConstantCollections;
import com.betterjr.modules.risk.loan.entity.LoanProjectRelation;
import com.betterjr.modules.risk.loan.entity.LoanTransaction;
import com.betterjr.modules.risk.loan.entity.LoanTransactionRelation;
import com.betterjr.modules.risk.loan.entity.TransactionDTO;
import com.google.common.collect.Lists;

/**
 * 
 * @ClassName: LoanTransactionRelationService 
 * @Description: 贷款项目交易数据关系表中间表
 * @author xuyp
 * @date 2018年6月26日 下午2:11:17 
 *
 */
@Service
public class LoanTransactionRelationService
        extends BaseService<LoanTransactionRelationMapper, LoanTransactionRelation> {

    @Autowired
    private LoanTransactionService transactionService;

    /**
     * 
     * @Title: queryLoanTransactionRelations 
     * @Description: 通过交易数据编号查询贷款项目
     * @param transactionNos
     * @return 
     * @throws 
     * @author xuyp
     * @date 2018年6月26日 上午11:29:43
     */
    public List<LoanTransactionRelation> queryLoanTransactionRelations(String transactionNos) {

        if (StringUtils.isNoneBlank(transactionNos)) {

            Map map = QueryTermBuilder.newInstance()
                    .put("businStatus", LoanTransactionRelationConstantCollections.RELATIONS_BUSINSTATUS_OK)
                    .put("transactionNo",
                            transactionNos.split(LoanProjectRelationConstantCollections.FILE_DATA_SEPATORY_CHAR))
                    .build();

            return this.selectByProperty(map, "id Desc");

        }

        return Lists.newArrayList();

    }

    /**
     * 
     * @Title: queryLoanTransactionRelations 
     * @Description: 通过贷款项目id查询贷款项目交易数据关系表
     * @param anLoanId
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月26日 上午11:38:16
     */
    public List<LoanTransactionRelation> queryLoanTransactionRelations(Long anLoanId) {

        if (anLoanId != null && anLoanId != 0l) {

            Map map = QueryTermBuilder.newInstance()
                    .put("businStatus", LoanTransactionRelationConstantCollections.RELATIONS_BUSINSTATUS_OK)
                    .put("loanId", anLoanId).build();

            List<LoanTransactionRelation> relations = this.selectByProperty(map, "id Desc");
            for (LoanTransactionRelation loanTransactionRelation : relations) {
                loanTransactionRelation.setTransaction(
                        transactionService.selectByPrimaryKey(loanTransactionRelation.getTransactionNo()));
            }
            return relations;

        }

        return Lists.newArrayList();

    }

    /**
     * 
     * @Title: saveAddRelation 
     * @Description: 新增交易数据与贷款项目关系表 
     * @param transactionNos
     * @param anLoan
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月26日 下午1:50:15
     */
    public List<LoanTransactionRelation> saveAddOrUpdateRelation(String transactionNos, LoanProjectRelation anLoan) {

        logger.info("LoanTransactionRelationService  saveAddOrUpdateRelation" + "  transactionNos:" + transactionNos
                + "  anLoan:" + anLoan);

        ArrayList<LoanTransactionRelation> list = Lists.newArrayList();

        if (StringUtils.isNoneBlank(transactionNos) && anLoan.getId() != null && anLoan.getId() != 0L) {

            for (String transactionNo : transactionNos
                    .split(LoanProjectRelationConstantCollections.FILE_DATA_SEPATORY_CHAR)) {

                List<LoanTransactionRelation> relations = queryLoanTransactionRelations(transactionNo);
                if (Collections3.isEmpty(relations)) {

                    LoanTransactionRelation relation = new LoanTransactionRelation();
                    relation.saveAddInitValue();
                    relation.setTransactionNo(transactionNo);
                    relation.setLoanId(anLoan.getId());
                    relation.setElcAggrementName(anLoan.getElcAggrementName());
                    relation.setLoanAggrementNo(anLoan.getLoanAggrementNo());

                    this.insertSelective(relation);
                    list.add(relation);
                } else {

                    for (LoanTransactionRelation loanTransactionRelation : relations) {
                        if (anLoan.getId() != loanTransactionRelation.getLoanId()) {
                            BTAssert.notNull(null, "当前交易数据已经绑定了贷款项目" + anLoan.getElcAggrementName());
                        } else {
                            loanTransactionRelation.setElcAggrementName(anLoan.getElcAggrementName());
                            loanTransactionRelation.setLoanAggrementNo(anLoan.getLoanAggrementNo());
                            this.updateByPrimaryKeySelective(loanTransactionRelation);
                            list.add(loanTransactionRelation);
                        }
                    }

                }

            }

        }

        return list;

    }

    /**
     * 
     * @Title: saveDeleteRelationsByLoanId 
     * @Description:作废关系数据通过贷款项目
     * @param anLoanId
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月26日 下午2:17:04
     */
    public List<LoanTransactionRelation> saveDeleteRelationsByLoanId(Long anLoanId) {

        List<LoanTransactionRelation> lists = Lists.newArrayList();
        if (anLoanId != null && anLoanId != 0L) {

            List<LoanTransactionRelation> relations = queryLoanTransactionRelations(anLoanId);
            for (LoanTransactionRelation relation : relations) {
                relation.setBusinStatus(LoanTransactionRelationConstantCollections.RELATIONS_BUSINSTATUS_DELETED);
                this.updateByPrimaryKeySelective(relation);
                lists.add(relation);
            }
        }

        return lists;

    }

    /**
     * 
     * @Title: saveOrUpdateTransaction 
     * @Description: 新增或者更新交易数据 
     * @param transactionLists TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年7月4日 上午10:19:22
     */
    public void saveOrUpdateTransaction(List<TransactionDTO> transactionLists) {

        for (TransactionDTO transactionDTO : transactionLists) {
            LoanTransaction anLoanTransaction = new LoanTransaction();
            anLoanTransaction.convertTransaction(transactionDTO);
            transactionService.saveOrUpdateLoanTransaction(anLoanTransaction);
        }

    }

}
